<script setup>
// import { artGetChannelsService } from '@/api/article'
import { Delete, Edit } from '@element-plus/icons-vue'
import { ref } from 'vue'
import ChannelEdit from './components/ChannelEdit.vue'
import { useArticleStore } from '@/stores/index'

const channelList = ref([])
const loading = ref(false)
const articleStore = useArticleStore()
// 定义一个空属性绑定组件
const dialog = ref()

const getChannelList = async () => {
  loading.value = true
  // const res = await artGetChannelsService()
  // 模拟接口请求等待
  setTimeout(() => {
    channelList.value = articleStore.channelList

    loading.value = false
  }, 500)
}
getChannelList()

const onDelChannel = async (row) => {
  await ElMessageBox.confirm('你确认要删除该分类吗', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消',
  })
  articleStore.removeChannelList(row.id)
  ElMessage.success('删除成功')
  getChannelList()
}
const onEditChannel = (row) => {
  console.log(row)

  dialog.value.open(row)
}
const onAddChannel = () => {
  // dialog.value 就是可以拿到组件
  dialog.value.open({})
}
// 回显页面
const onSuccess = () => {
  getChannelList()
}
</script>

<template>
  <page-container title="文章分类">
    <template #extra>
      <el-button @click="onAddChannel">添加分类</el-button>
    </template>

    <el-table v-loading="loading" :data="channelList" style="width: 100%">
      <el-table-column type="index" label="序号" width="100"> </el-table-column>
      <el-table-column prop="cate_name" label="分类名称"> </el-table-column>
      <el-table-column prop="cate_alias" label="分类别名"> </el-table-column>
      <el-table-column label="操作" width="150">
        <!-- row 就是 channelList的每一项 ，@index 下标-->
        <template #default="{ row, $index }">
          <el-button
            :icon="Edit"
            @click="onEditChannel(row, $index)"
            type="primary"
            circle
            plain
          ></el-button>
          <el-button
            :icon="Delete"
            @click="onDelChannel(row, $index)"
            type="danger"
            circle
            plain
          ></el-button>
        </template>
      </el-table-column>

      <template>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <!-- 给组件绑定一个ref 才可以拿到里面暴露的方法 -->
    <ChannelEdit ref="dialog" @success="onSuccess"></ChannelEdit>
  </page-container>
</template>
<style lang="scss" scoped></style>
